﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevTeam;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Sistem
{
    public partial class Cari : XForm
    {
        public Cari()
        {
            InitializeComponent();
        }

        //bool isAks = MyDept == 0;
        BindingSource bsC, bsMM, bsCust, bsVend, bsAtelye, bsMarka, bsAdres, bsBanka, bsEmp, bsDag;
        private void Cari_Load(object sender, EventArgs e)
        {
            
            TAM.CariTableAdapter = new Datasets.MyDataTableAdapters.CariTableAdapter();
            TAM.CustomerTableAdapter = new Datasets.MyDataTableAdapters.CustomerTableAdapter();
            TAM.BrandTableAdapter = new Datasets.MyDataTableAdapters.BrandTableAdapter();
            TAM.VendorTableAdapter = new Datasets.MyDataTableAdapters.VendorTableAdapter();
            TAM.AtelyeTableAdapter = new Datasets.MyDataTableAdapters.AtelyeTableAdapter();
            TAM.EmpCustomerTableAdapter = new Datasets.MyDataTableAdapters.EmpCustomerTableAdapter();
            TAM.DagilimCustomerTableAdapter = new Datasets.MyDataTableAdapters.DagilimCustomerTableAdapter();
            TAM.AddressTableAdapter = new Datasets.MyDataTableAdapters.AddressTableAdapter();
            TAM.BankAccountTableAdapter = new Datasets.MyDataTableAdapters.BankAccountTableAdapter();

            GetDetails("Cari;Customer;Vendor;Atelye;Brand;Address;BankAccount;EmpCustomer;DagilimCustomer;xBuffer", "GetCari");

            ds.Cari.Columns.Add("M", typeof(string), "Iif(COUNT(Child(FK_Customer_Cari_CariId).CariId)>0, 'M', null)");
            ds.Cari.Columns.Add("Ü", typeof(string), "Iif(COUNT(Child(FK_Vendor_Cari_CariId).CariId)>0, 'Ü', null)");
            ds.Cari.Columns.Add("A", typeof(string), "Iif(COUNT(Child(FK_Atelye_Cari_CariId).CariId)>0, 'A', null)");

            bsC = new BindingSource(ds, "Cari");
            bsCust = new BindingSource(bsC, "FK_Customer_Cari_CariId");
            bsVend = new BindingSource(bsC, "FK_Vendor_Cari_CariId");
            bsAtelye = new BindingSource(bsC, "FK_Atelye_Cari_CariId");

            bsMarka = new BindingSource(bsCust, "FK_Brand_Customer_CustomerId");
            bsAdres = new BindingSource(bsCust, "FK_Address_Customer_CustomerId");
            bsBanka = new BindingSource(bsCust, "FK_BankAccount_Customer_CustomerId");
            bsEmp = new BindingSource(bsCust, "FK_EmpCustomer_Customer_CustomerId");
            bsDag = new BindingSource(bsCust, "FK_DagilimCustomer_Customer_CustomerId");

            bsMM = new BindingSource(ds, "xBuffer");

            AddBsToList(bsC, bsCust, bsVend, bsAtelye, bsAdres, bsBanka, bsMarka, bsEmp, bsDag);

            InitGridView(gvCari, bsC);
            InitGridView(gvCust, bsCust);
            InitGridView(gvVend, bsVend);
            InitGridView(gvAtelye, bsAtelye);

            InitGridView(gvCAddress, bsAdres);
            InitGridView(gvCBankAccount, bsBanka);
            InitGridView(gvBrand, bsMarka);
            InitGridView(gvMT, bsEmp);
            InitGridView(gvDagilim, bsDag);
            InitGridView(gvMusteriMarka, bsMM);
            gvMT.Columns["UserId"].OptionsColumn.AllowEdit = true;

            ds.nFirma.Merge(ExecuteDataTable("GetNebim", "Firma"));
            gvCari.Columns["nFirmaId"].ColumnEdit = GetNebimRI(ds.nFirma, "nFirmaID");

            gvMT.MakeReadOnly();
            gvCari.AddSummary();
            gvMusteriMarka.AddSummary();

            gvBrand.Columns["BrandId"].ColumnEdit = new RepositoryItemTextEdit();
            gvCust.Columns["CustomerId"].ColumnEdit = new RepositoryItemTextEdit();
            gvVend.Columns["VendorId"].ColumnEdit = new RepositoryItemTextEdit();
            gvAtelye.Columns["AtelyeId"].ColumnEdit = new RepositoryItemTextEdit();
            gvCari.Columns["CariId"].ColumnEdit = new RepositoryItemTextEdit();

            gvBrand.Columns["Deleted"].Visible =
            gvCust.Columns["Deleted"].Visible =
            gvAtelye.Columns["Deleted"].Visible =
            gvVend.Columns["Deleted"].Visible = true;

            gvCust.Columns["CariId"].Visible =
            gvVend.Columns["CariId"].Visible =
            gvAtelye.Columns["CariId"].Visible =
            gvCust.Columns["Name"].Visible =
            gvVend.Columns["Name"].Visible =
            gvAtelye.Columns["Name"].Visible =
            gvCAddress.Columns["CustomerId"].Visible =
            gvCBankAccount.Columns["CustomerId"].Visible =
            gvDagilim.Columns["CustomerId"].Visible =
            gvMT.Columns["CustomerId"].Visible =
            gvBrand.Columns["CustomerId"].Visible =
            gvMusteriMarka.Columns["intVal"].Visible = gvMusteriMarka.Columns["decVal"].Visible = gvMusteriMarka.Columns["photoVal"].Visible = false;
            gvMusteriMarka.SetCaptions("strVal:MusteriMarka");

            //gvMusteriMarka.Columns["strVal"].OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;

            gvCari.OptionsView.NewItemRowPosition = gvDagilim.OptionsView.NewItemRowPosition = gvMT.OptionsView.NewItemRowPosition = gvCAddress.OptionsView.NewItemRowPosition = gvCBankAccount.OptionsView.NewItemRowPosition = gvBrand.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            gvCari.Columns["M"].VisibleIndex = 
            gvCari.Columns["Ü"].VisibleIndex = 
            gvCari.Columns["A"].VisibleIndex = 2;

            sccCari.AutoSize();
            gvCari.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gvCari_FocusedRowChanged);
            ExpandCollapse(sccCari, AcKapa.Collapse);

            gvCari_FocusedRowChanged(null, null);

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            chkMusteri.CheckedChanged += new EventHandler(chkMusteri_CheckedChanged);
            chkUretici.CheckedChanged += new EventHandler(chkUretici_CheckedChanged);
            chkAtelye.CheckedChanged += new EventHandler(chkAtelye_CheckedChanged);

            evenrow = gvCari.Appearance.EvenRow;
            oddrow = gvCari.Appearance.OddRow;
            gvCari.OptionsView.EnableAppearanceOddRow = gvCari.OptionsView.EnableAppearanceEvenRow = false;
            gvCari.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(gvCari_RowStyle);

            gvCari.DoubleClick += new EventHandler(gvCari_DoubleClick);

            gvCari.BestFitColumns();
            gvMusteriMarka.BestFitColumns();

            sccAtelye.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
            sccUretici.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
            
            gvCust.OptionsBehavior.EditorShowMode = 
            gvVend.OptionsBehavior.EditorShowMode = 
            gvAtelye.OptionsBehavior.EditorShowMode = 
            gvBrand.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDown;

            gvCari.RowCellStyle += new RowCellStyleEventHandler(gvCari_RowCellStyle);
            gvCari.DisableRowDelete();
            
            //TODO: Mehtap hanıma, Aks. üreticisi ekleme yetkisi verelim, sadece Ü gözüksün!
            //if (!isAdmin)
            //    gvCari.MakeColumnInvisible("M", "A");
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Save();
            GetDetails("Cari;Customer;Vendor;Atelye", "UpdCariNames");
        }

        void gvCari_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "Name")
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void gvCari_DoubleClick(object sender, EventArgs e)
        {
            GridView view = (GridView)sender;
            Point pt = view.GridControl.PointToClient(Control.MousePosition);
            GridHitInfo info = view.CalcHitInfo(pt);
            if (info.RowHandle > -1)
                switch (gvCari.FocusedColumn.FieldName)
                {
                    case "M": xtraTabControl1.SelectedTabPage = tpMusteri; break;
                    case "A": xtraTabControl1.SelectedTabPage = tpAtelye; break;
                    case "Ü": xtraTabControl1.SelectedTabPage = tpUretici; break;
                }
        }

        AppearanceObject evenrow, oddrow;
        void gvCari_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.RowHandle > -1)
            {
                if (e.RowHandle == view.FocusedRowHandle)
                    e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
                else
                    e.Appearance.Combine((e.RowHandle & 1) != 0 ? evenrow : oddrow);
            }

        }

        void chkMusteri_CheckedChanged(object sender, EventArgs e)
        {
            if (chkMusteri.Checked)
            {
                MyData.CustomerRow cr = ds.Customer.NewCustomerRow();
                cr.Name = strParse(bsC, "Name");
                cr.CariId = intParse(bsC, "CariId");
                ds.Customer.AddCustomerRow(cr);

                sccMusteri.Visible = true;
                chkMusteri.Visible = false;
            }
        }

        void chkUretici_CheckedChanged(object sender, EventArgs e)
        {
            if (chkUretici.Checked)
            {
                MyData.VendorRow cr = ds.Vendor.NewVendorRow();
                cr.Name = strParse(bsC, "Name");
                cr.CariId = intParse(bsC, "CariId");
                ds.Vendor.AddVendorRow(cr);

                sccUretici.Visible = true;
                chkUretici.Visible = false;
            }
        }

        void chkAtelye_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAtelye.Checked)
            {
                MyData.AtelyeRow cr = ds.Atelye.NewAtelyeRow();
                cr.Name = strParse(bsC, "Name");
                cr.CariId = intParse(bsC, "CariId");
                ds.Atelye.AddAtelyeRow(cr);

                sccAtelye.Visible = true;
                chkAtelye.Visible = false;
            }
        }

        void gvCari_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gvCust.BestFitColumns();
            gvBrand.BestFitColumns();
            gvAtelye.BestFitColumns();
            gvVend.BestFitColumns();

            bool bCust, bVend, bAtelye;
            bCust = gvCust.RowCount > 0;
            bVend = gvVend.RowCount > 0;
            bAtelye = gvAtelye.RowCount > 0;

            sccMusteri.Visible = bCust;
            chkMusteri.Visible = !bCust;

            sccUretici.Visible = bVend;
            chkUretici.Visible = !bVend;

            sccAtelye.Visible = bAtelye;
            chkAtelye.Visible = !bAtelye;

            txtInfo.Caption = strParse(bsC, "Name");
        }

    }
}
